# -*- coding: utf-8 -*-
import DBLink.dbHelp

dbHelp = DBLink.dbHelp

# 查询--类别信息
def _api_goodstype_select_all(_request):
    '''
           /api/goodstype.select.all
           功能：查询--类别信息
           编写：cgz
           :param _request ={"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""}}
           :return _respon ={"ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}, "IsError": false, "ReturnValue": true,
           "TB_GoodsTypeList": [{"GoodsTypeName": "马桶", "GoodsTypeID": 1}, {"GoodsTypeName": "瓷砖", "GoodsTypeID": 2}, {"GoodsTypeName": "浴缸", "GoodsTypeID": 3}]}
            :param  _respon["TB_GoodsTypeList"]:返回集合
           :param _respon["GoodsTypeName"]:类型名称
           :param _respon["GoodsTypeID"]:类型ID
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    strSqlSel = """
        SELECT
        T1.GoodsTypeID,T1.GoodsTypeName
        FROM
        goodstype T1
        WHERE
        T1.IsUsing='1'
    """
    strSqlSelRe = dbHelp.Select(strSqlSel)
    _respon["IsError"] = strSqlSelRe["IsError"]
    _respon["ErrorInfo"]["ErrorCode"] = strSqlSelRe["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = strSqlSelRe["ErrorMsg"]
    _respon["ReturnValue"] = strSqlSelRe["ReturnValue"]
    _respon["TB_GoodsTypeList"] = strSqlSelRe["Value"]
    return _respon


# 添加---类型对应商品图片
def _api_goodstype_type_picture_add(_request):
    '''
           /api/goodstype.type.picture.add
           功能：添加---类型对应商品图片
           编写：cgz
           :param _request ={"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""}, "ImgRemark": "测试描述", "GoodsTypeID": "1", "ImgPath": "image/222.img", "ImgName": "测试名称"}
           :param _request["ImgRemark"]=图片描述
           :param _request["GoodsTypeID"]=类型ID
           :param _request["ImgPath"]=图片路径
           :param _request["ImgName"]=图片名称
           :return _respon ={"ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}, "IsError": false, "ReturnValue": true}
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    objIdReturn = dbHelp.GetNewids("goodstypeimg")
    strSqlIn = """
        INSERT INTO goodstypeimg(
        ImgID,ImgRemark,GoodsTypeID
        ,ImgPath,ImgName,IsUsing,UploadTime
        )
        VALUES
        (
        '""" + objIdReturn["Value"] + """',
       '""" + str(_request["ImgRemark"]) + """',
        '""" + str(_request["GoodsTypeID"]) + """',
        '""" + str(_request["ImgPath"]) + """',
       '""" + str(_request["ImgName"]) + """',
        '1',
         DATE_FORMAT(now(),'%Y-%m-%d %H:%i:%s')
        )
    """
    ReturnSqlIn = dbHelp.Exec(strSqlIn)
    _respon["IsError"] = ReturnSqlIn["IsError"]
    _respon["ErrorInfo"]["ErrorCode"] = ReturnSqlIn["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = ReturnSqlIn["ErrorMsg"]
    _respon["ReturnValue"] = ReturnSqlIn["ReturnValue"]
    return _respon


# 查询---类型对应商品图片
def _api_goodstype_type_picture_select(_request):
    '''
       /api/goodstype.type.picture.select
       功能：查询---类型对应商品图片
       编写：cgz
       :param _request ={"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""}, "GoodsTypeID": "2"}
       :param _request["GoodsTypeID"]=类型ID
       :return _respon ={"ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}, "IsError": false, "ReturnValue": true,
       "TB_TypePictureList": [{"ImgName": "测试名称", "ImgID": 2, "ImgRemark": "测试描述", "GoodsTypeID": 2, "ImgPath": "image/222.img"},
       {"ImgName": "测试3", "ImgID": 3, "ImgRemark": "测试3", "GoodsTypeID": 2, "ImgPath": "image/333.img"}]}
        :param _respon["TB_TypePictureList"]:返回集合
            :param _respon["ImgName"]:图片名称
            :param _respon["ImgID"]:图片ID
            :param _respon["ImgRemark"]:位置图片描述
            :param _respon["GoodsTypeID"]:类型ID
            :param _respon["ImgPath"]:图片路径
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    objIdReturn = dbHelp.GetNewids("goodstypeimg")
    # strSqlSel = """
    #     SELECT
    #     T1.ImgID,T1.ImgRemark,T1.ImgName,T1.ImgPath,T1.GoodsTypeID
    #     FROM
    #     goodstypeimg T1
    #     WHERE
    #     T1.GoodsTypeID= '""" + str(_request["GoodsTypeID"]) + """'
    #     AND
    #     T1.IsUsing='1'
    #     ORDER BY RAND()  LIMIT 6
    # """
    strSqlSel ="""
        SELECT
        T1.ImgPath,T1.ImgName
        FROM
        gallerypicturelist T1
        WHERE
        T1.GoodsTypeID = '""" + str(_request["GoodsTypeID"]) + """'
        AND
        T1.IsUsing='1'
        ORDER BY RAND()  LIMIT 6
    """

    ReturnSqlSel = dbHelp.Select(strSqlSel)
    _respon["IsError"] = ReturnSqlSel["IsError"]
    _respon["ErrorInfo"]["ErrorCode"] = ReturnSqlSel["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = ReturnSqlSel["ErrorMsg"]
    _respon["ReturnValue"] = ReturnSqlSel["ReturnValue"]
    _respon["TB_TypePictureList"] = ReturnSqlSel["Value"]
    return _respon


# 删除---类型对应商品图片
def _api_goodstype_type_picture_delete(_request):
    '''
          /api/goodstype.type.picture.delete
           功能：删除---类型对应商品图片
           编写：zpp
            :param _request = {"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""},"ImgID":1}
             :param _request["ImgID"]:图片ID
           :return _respon ={"RowNum": 3, "ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}], "IsError": false, "ReturnValue": true}
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}

    strsql = """
            update goodstypeimg T
            set T.IsUsing = 0
            where T.ImgID = """ + str(_request["ImgID"]) + """
         """
    objReturn = dbHelp.Exec(strsql)
    _respon["ErrorInfo"]["ErrorCode"] = objReturn["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = objReturn["ErrorMsg"]
    _respon["IsError"] = objReturn["IsError"]
    _respon["ReturnValue"] = objReturn["ReturnValue"]
    return _respon


# 新增---类型，图片（类型不存在时使用）
def _api_goodstype_type_add(_request):
    '''
           /api/goodstype.type.add
           功能：新增---类型，图片（类型不存在时使用）
           编写：zpp
            :param _request = {"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""}, "GoodsTypeName": "没有的类型21", "ImgRemark": "图片描述", "ImgPath": "路径", "ImgName": "图片名"}
            :param _request["GoodsTypeName"]:类型名称
             :param _request["ImgRemark"]:图片描述
             :param _request["ImgPath"]:图片路经
             :param _request["ImgName"]:图片名称
           :return _respon ={"ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}, "IsError": false, "ReturnValue": true}
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    strsql = """
                    select COUNT(*)AS num
                    FROM
                    goodstype T
                    WHERE T.GoodsTypeName =  '""" + _request["GoodsTypeName"] + """'
       """
    objReturn = dbHelp.Select(strsql)
    if objReturn["Value"][0]["num"] != 0:
        _respon["IsError"] = True
        _respon["ErrorInfo"]["ErrorCode"] = 1
        _respon["ErrorInfo"]["ErrorMsg"] = "您输入的类型名称已经存在！请重新输入！谢谢"
        _respon["ReturnValue"] = False
    else:
        objIdReturn = dbHelp.GetNewids("goodstype")
        if objIdReturn["Value"] == 0:
            _respon["IsError"] = objIdReturn["IsError"]
            _respon["ErrorInfo"]["ErrorCode"] = objIdReturn["ErrorCode"]
            _respon["ErrorInfo"]["ErrorMsg"] = objIdReturn["ErrorMsg"]
            _respon["ReturnValue"] = objIdReturn["ReturnValue"]
        else:
            strSqlB = """
                 INSERT INTO goodstype
                 (GoodsTypeID,GoodsTypeName,IsUsing)
                VALUES(
                '""" + objIdReturn["Value"] + """',
               '""" + _request["GoodsTypeName"] + """',
                '1'
                )
                 """
            objReturnB = dbHelp.Exec(strSqlB)
            if objReturnB["ReturnValue"]==True:
                #添加图片
                objIdReturns = dbHelp.GetNewids("goodstypeimg")
                strSqlC = """
                    INSERT INTO goodstypeimg
                        (
                        ImgID,ImgRemark,GoodsTypeID,ImgPath,ImgName,IsUsing,UploadTime
                        )
                        VALUES
                        (
                        '""" + objIdReturns["Value"] + """',
                       '""" + str(_request["ImgRemark"]) + """',
                        '""" + str(objIdReturn["Value"]) + """',
                         '""" + str(_request["ImgPath"]) + """',
                        '""" + str(_request["ImgName"]) + """',
                        '1',
                        DATE_FORMAT(now(),'%Y-%m-%d %H:%i:%s')
                        )
                     """
                objReturnC = dbHelp.Exec(strSqlC)
                _respon["IsError"] = objReturnC["IsError"]
                _respon["ErrorInfo"]["ErrorCode"] = objReturnC["ErrorCode"]
                _respon["ErrorInfo"]["ErrorMsg"] = objReturnC["ErrorMsg"]
                _respon["ReturnValue"] = objReturnC["ReturnValue"]
            else:
                _respon["IsError"] = objReturnB["IsError"]
                _respon["ErrorInfo"]["ErrorCode"] = objReturnB["ErrorCode"]
                _respon["ErrorInfo"]["ErrorMsg"] = objReturnB["ErrorMsg"]
                _respon["ReturnValue"] = objReturnB["ReturnValue"]
    return _respon


#新增--类型（只添加类型，不添加图片）
def _api_goodstype_add(_request):
    '''
          /api/goodstype.add
           功能：新增--类型（只添加类型，不添加图片）
           编写：zpp
            :param _request = {"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""},"GoodsTypeName":1}
             :param _request["GoodsTypeName"]:类型名称
           :return _respon ={"RowNum": 3, "ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}], "IsError": false, "ReturnValue": true}
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    strsql = """
                      select COUNT(*)AS num
                      FROM
                      goodstype T
                      WHERE T.GoodsTypeName =  '""" + _request["GoodsTypeName"] + """'
                      and
                      T.IsUsing='1'
         """
    objReturn = dbHelp.Select(strsql)
    if objReturn["Value"][0]["num"] != 0:
        _respon["IsError"] = True
        _respon["ErrorInfo"]["ErrorCode"] = 1
        _respon["ErrorInfo"]["ErrorMsg"] = "您输入的类型名称已经存在！请重新输入！谢谢"
        _respon["ReturnValue"] = False
    else:
        objIdReturn = dbHelp.GetNewids("goodstype")
        if objIdReturn["Value"] == 0:
            _respon["IsError"] = objIdReturn["IsError"]
            _respon["ErrorInfo"]["ErrorCode"] = objIdReturn["ErrorCode"]
            _respon["ErrorInfo"]["ErrorMsg"] = objIdReturn["ErrorMsg"]
            _respon["ReturnValue"] = objIdReturn["ReturnValue"]
        else:
            strSqlB = """
                   INSERT INTO goodstype
                   (GoodsTypeID,GoodsTypeName,IsUsing)
                  VALUES(
                  '""" + objIdReturn["Value"] + """',
                 '""" + _request["GoodsTypeName"] + """',
                  '1'
                  )
                   """
            objReturnB = dbHelp.Exec(strSqlB)
            _respon["ErrorInfo"]["ErrorCode"] = objReturnB["ErrorCode"]
            _respon["ErrorInfo"]["ErrorMsg"] = objReturnB["ErrorMsg"]
            _respon["IsError"] = objReturnB["IsError"]
            _respon["ReturnValue"] = objReturnB["ReturnValue"]
    return _respon


# 查询--类型模糊查询
def _api_goodstype_type_mohu_select(_request):
    '''
           /api/goodstype.type.mohu.select
           功能：查询--类型模糊查询
           编写：cgz
           :param _request ={"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""}, "ImgName": "测试"}
           :return _respon ={"ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}, "TB_GoodsTypeListPic": [{"ImgName": "测试名称", "ImgID": 2, "ImgRemark": "测试描述", "GoodsTypeID": 2, "ImgPath": "D:/ImageUp_A/imgUp_20170406160446.png"},
           {"ImgName": "测试3", "ImgID": 3, "ImgRemark": "测试3", "GoodsTypeID": 2, "ImgPath": "D:/ImageUp_A/imgUp_20170406160446.png"}], "IsError": false, "ReturnValue": true}
            :param  _respon["TB_GoodsTypeListPic"]:返回集合
           :param _respon["ImgID"]:图片ID
           :param _respon["ImgName"]:图片名称
            :param _respon["ImgRemark"]:图片描述
           :param _respon["ImgPath"]:图片路径
           :param _respon["GoodsTypeID"]:类型ID
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    strSqlSel = """
		SELECT
         T1.ImgID,T1.ImgName,T1.ImgRemark,T1.ImgPath,T1.GoodsTypeID
        FROM
        goodstypeimg T1
        WHERE
        T1.ImgName LIKE '%""" + _request["ImgName"] + """%'
        AND
        T1.IsUsing='1'
    """
    strSqlSelRe = dbHelp.Select(strSqlSel)
    if strSqlSelRe["RowNum"]!=0:
        _respon["IsError"] = strSqlSelRe["IsError"]
        _respon["ErrorInfo"]["ErrorCode"] = strSqlSelRe["ErrorCode"]
        _respon["ErrorInfo"]["ErrorMsg"] = strSqlSelRe["ErrorMsg"]
        _respon["ReturnValue"] = strSqlSelRe["ReturnValue"]
        _respon["TB_GoodsTypeListPic"] = strSqlSelRe["Value"]
    else:
        _respon["IsError"] = True
        _respon["ErrorInfo"]["ErrorCode"] = 1
        _respon["ErrorInfo"]["ErrorMsg"] = "无找到任何结果！"
        _respon["ReturnValue"] = False
    return _respon


#修改--类型对应图片信息
def _api_goodstype_type_picture_information_edit(_request):
    '''
           /api/goodstype.type.picture.information.edit
           功能：修改--类型对应图片信息
           编写：cgz
           :param _request ={"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""}, "ImgRemark": "3", "ImgName": "4", "ImgID": "1", "ImgRemark_Old": "1", "ImgName_Old": "2"}
           :param _respon["ImgRemark"]:图片描述
           :param _respon["ImgName"]:图片名称
           :param _respon["ImgID"]:图片ID
           :param _respon["ImgRemark_Old"]:旧图片描述
           :param _respon["ImgName_Old"]:旧图片名称
           :return _respon ={"ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}, "TB_GoodsTypeListPic": [{"ImgName": "测试名称", "ImgID": 2, "ImgRemark": "测试描述", "GoodsTypeID": 2, "ImgPath": "D:/ImageUp_A/imgUp_20170406160446.png"},
           {"ImgName": "测试3", "ImgID": 3, "ImgRemark": "测试3", "GoodsTypeID": 2, "ImgPath": "D:/ImageUp_A/imgUp_20170406160446.png"}], "IsError": false, "ReturnValue": true}
            :param  _respon["TB_GoodsTypeListPic"]:返回集合
           :param _respon["ImgID"]:图片ID
           :param _respon["ImgName"]:图片名称
            :param _respon["ImgRemark"]:图片描述
           :param _respon["ImgPath"]:图片路径
           :param _respon["GoodsTypeID"]:类型ID
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    strSqlUpdate = """
        UPDATE goodstypeimg T1
        SET T1.ImgRemark='""" + str(_request["ImgRemark"]) + """',
        T1.ImgName='""" + str(_request["ImgName"]) + """',
        T1.UploadTime=DATE_FORMAT(now(),'%Y-%m-%d %H:%i:%s')
        WHERE
        T1.ImgID='""" + str(_request["ImgID"]) + """'
        AND
        T1.ImgRemark='""" + str(_request["ImgRemark_Old"]) + """'
        AND
        T1.ImgName='""" + str(_request["ImgName_Old"]) + """'
        AND
        T1.IsUsing='1'
    """
    strSqlSelRe = dbHelp.Exec(strSqlUpdate)
    if  strSqlSelRe["ReturnValue"]==True:
        _respon["IsError"] = strSqlSelRe["IsError"]
        _respon["ErrorInfo"]["ErrorCode"] = strSqlSelRe["ErrorCode"]
        _respon["ErrorInfo"]["ErrorMsg"] = strSqlSelRe["ErrorMsg"]
        _respon["ReturnValue"] = strSqlSelRe["ReturnValue"]
    else:
        _respon["IsError"] = True
        _respon["ErrorInfo"]["ErrorCode"] = 1
        _respon["ErrorInfo"]["ErrorMsg"] = "无作任何修改！"
        _respon["ReturnValue"] = False
    return _respon

# 删除---类型
def _api_goodstype_type_del(_request):
    '''
           /api/goodstype.type.del
           功能：停用---类型
           编写：cgz
           :param _request ={"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""},"GoodsTypeID":"15"}
            :param _respon["GoodsTypeID"]:类型ID
            :param  _respon={"ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}, "IsError": false, "ReturnValue": true}
    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    strSqlDel = """
        UPDATE goodstype T1
        SET T1.IsUsing='0'
        WHERE
        T1.IsUsing='1'
        AND
        T1.GoodsTypeID='""" + str(_request["GoodsTypeID"]) + """'
    """
    strSqlSelDel = dbHelp.Exec(strSqlDel)
    _respon["IsError"] = strSqlSelDel["IsError"]
    _respon["ErrorInfo"]["ErrorCode"] = strSqlSelDel["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = strSqlSelDel["ErrorMsg"]
    _respon["ReturnValue"] = strSqlSelDel["ReturnValue"]

    return _respon
# 编辑---类型
def _api_goodstype_type_edit(_request):
    '''
          /api/goodstype.type.edit
           功能：修改-类型名称           编写：zpp
            :param _request = {"RequestBaseHeader": {"SeesionKey": "", "TimeSpan": ""},"GoodsTypeName":"瓷砖","GoodsTypeName_Old":"陶瓷","GoodsTypeID":"1"}
             :param _request["GoodsTypeID"]:类型ID
             :param _request["GoodsTypeName"]:类型I名称
             :param _request["GoodsTypeName_Old"]:类型名称旧
           :return _respon ={ "ErrorInfo": {"ErrorCode": 0, "ErrorMsg": ""}}], "IsError": false, "ReturnValue": true}

    '''
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}
    ReturnIfSql = """
            SELECT
            COUNT(*) AS num
            FROM
            goodstype T1
            WHERE T1.GoodsTypeName='""" + str(_request["GoodsTypeName"]) + """'
            AND T1.GoodsTypeID='""" + str(_request["GoodsTypeID"]) + """'
            AND T1.IsUsing=1

        """
    objReturnIf = dbHelp.Select(ReturnIfSql)
    if objReturnIf["Value"][0]["num"] != 0:
        _respon["IsError"] = True
        _respon["ErrorInfo"]["ErrorCode"] = 1
        _respon["ErrorInfo"]["ErrorMsg"] = '无做任何修改！'
        _respon["ReturnValue"] = False
    else:
        strsqlS = """
                     SELECT	count(*) AS numcount
                    FROM	  goodstype
                    WHERE	goodstype.IsUsing = 1
                    AND		goodstype.GoodsTypeName = '""" + _request["GoodsTypeName"] + """'
                    and goodstype.GoodsTypeID != '""" + _request["GoodsTypeID"] + """'
                       """
        objReturnS = dbHelp.Select(strsqlS)
        if objReturnS["Value"][0]["numcount"] != 0:
            _respon["IsError"] = True
            _respon["ErrorInfo"]["ErrorCode"] = 1
            _respon["ErrorInfo"]["ErrorMsg"] = "您输入的类型名称已经存在！请重新输入一个！谢谢"
            _respon["ReturnValue"] = False
        else:
            strsql1 = """
                update goodstype T
                set T.GoodsTypeName= '""" + _request["GoodsTypeName"] + """'
                where T.GoodsTypeID =  '""" + _request["GoodsTypeID"] + """'
                and (
                    T.GoodsTypeName='""" + _request["GoodsTypeName_Old"] + """'
                )
                 """
            objReturn1 = dbHelp.Exec(strsql1)
            _respon["ErrorInfo"]["ErrorCode"] = objReturn1["ErrorCode"]
            _respon["ErrorInfo"]["ErrorMsg"] = objReturn1["ErrorMsg"]
            _respon["IsError"] = objReturn1["IsError"]
            _respon["ReturnValue"] = objReturn1["ReturnValue"]
    return _respon